package com.duqc.quartz;

import com.duqc.util.DateUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.stereotype.Service;

/**
 * @ClassName SchedulerJob
 * @Description 任务执行类
 * @Author duqc
 * @Date 2021/9/7
 **/
@Slf4j
@Service
@RequiredArgsConstructor
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class SchedulerJob implements Job {

    @Override
    public void execute(JobExecutionContext context) {
        // 取得job详情
        JobDetail jobDetail = context.getJobDetail();
        // 取得job名称
        String jobName = jobDetail.getKey().getName();
        String jobGroup = jobDetail.getKey().getGroup();
        log.info("=========定时任务 |  任务分组：" + jobGroup + "，任务名称：" + jobName + "，调用接口服务执行开始=========");
        log.info("定时任务名称: " + jobName);
        // 取得job的类
        log.info("定时任务执行类名: " + jobDetail.getJobClass());
        // 取得job开始时间
        log.info(jobName + " 开始时间： " + DateUtils.format(context.getFireTime(), "yyyy-MM-dd HH:mm:ss"));
        // 取得job下次触发时间
        System.out.println(context.getNextFireTime());
        if (null != context.getNextFireTime()) {
            log.info("下次执行时间： " + DateUtils.format(context.getNextFireTime(), "yyyy-MM-dd HH:mm:ss"));
        }
        //如果下次执行时间为空 可修改当前任务的执行状态为已完成
        JobDataMap dataMap = jobDetail.getJobDataMap();
        String rowGuid = dataMap.getString("rowGuid");
        log.info(rowGuid);
    }
}
